/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import l1j.server.server.model.Instance.L1PcInstance;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_OwnCharPack extends ServerBasePacket {

	private static final String S_OWN_CHAR_PACK = "[S] S_OwnCharPack";
	private static final int STATUS_INVISIBLE = 2;
	private static final int STATUS_PC = 4;
	private static final int STATUS_BRAVE = 16;
	private static final int STATUS_ELFBRAVE = 32;
	private static final int STATUS_FASTMOVABLE = 64;
	private static final int STATUS_GHOST = 128;

	private byte[] _byte = null;

	public S_OwnCharPack(final L1PcInstance pc) {
		this.buildPacket(pc);
	}

	private void buildPacket(final L1PcInstance pc) {

		int status = STATUS_PC;

		// グール毒みたいな緑の毒
		// if (pc.isPoison()) {
		// status |= STATUS_POISON;
		// }

		if (pc.isInvisble() || pc.isGmInvis()) {
			status |= STATUS_INVISIBLE;
		}
		if (pc.isBrave()) {
			status |= STATUS_BRAVE;
		}
		if (pc.isElfBrave()) {
			// エルヴンワッフルの場合は、STATUS_BRAVEとSTATUS_ELFBRAVEを立てる。
			// STATUS_ELFBRAVEのみでは効果が無い？
			status |= STATUS_BRAVE;
			status |= STATUS_ELFBRAVE;
		}
		if (pc.isFastMovable()) {
			status |= STATUS_FASTMOVABLE;
		}
		if (pc.isGhost()) {
			status |= STATUS_GHOST;
		}

		this.writeC(S_OPCODE_CHARPACK);
		this.writeH(pc.getX());
		this.writeH(pc.getY());
		this.writeD(pc.getId());
		//		if (pc.isDead()) {
		//			this.writeH(pc.getTempCharGfxAtDead());
		//		} else {
		//			this.writeH(pc.getTempCharGfx());
		//		}
		//		if (pc.isDead()) {
		//			this.writeC(pc.getStatus());
		//		} else {
		//			this.writeC(pc.getCurrentWeapon());
		//		}
		writeH(pc.isDead() ? pc.getTempCharGfxAtDead() : pc.getTempCharGfx());
		writeC(pc.isDead() ? pc.getStatus() : pc.getCurrentWeapon());
		this.writeC(pc.getOwnLightSize());
		this.writeC(pc.getHeading());
		this.writeC(pc.getMoveSpeed());
		//		this.writeExp(pc.getExp());
		writeD(1);
		this.writeH(pc.getLawful());
		this.writeS(pc.getName());
		this.writeS(pc.getTitle());
		this.writeC(status);
		this.writeD(pc.getClanid());
		this.writeS(pc.getClanname()); // クラン名
		this.writeS(null); // ペッホチング？
		this.writeC(0); // ？
		if (pc.isInParty()){ // パーティー中
			final int Hprate = 100 * pc.getCurrentHp() / pc.getMaxHp();
			this.writeC(Hprate);
		} else {
			this.writeC(0xFF);
		}
		if (pc.isDrink()) { // liquorで酔っている
			writeC(0x01);
		}
		else if (pc.isSpeedUp()) {
			writeC(0x08); // 3段加速
		} else {
			writeC(0);
		}
		this.writeC(0); // PC = 0, Mon = Lv
		this.writeC(0); // ？
		this.writeC(0xFF);
		this.writeC(0xFF);
		this.writeS(null);
		this.writeC(0);
	}

	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return S_OWN_CHAR_PACK;
	}

}